/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import database.DBNames;

/**
 *
 * @author falxa
 */
public class Term 
{
    private int id;
    private String value;
    private HashMap<Integer, ArrayList<Integer>> positions; // [ (Noeud n, pos x), (Noeud n, pos y), (Noeud m, pos z), ...)
    private HashMap<Integer, Double> weights;
    
    public Term(int id, String value) {
        this.id = id;
        this.value = value;
        this.positions = new HashMap<>();
        this.weights = new HashMap<>();
    }
    
    public Term(String value) {
    	this.value = value;
    	this.positions = new HashMap<>();
    }
    
    public Term(ResultSet rs)
    {
    	try
		{
			this.id = rs.getInt(DBNames.TERM_ID);
			this.value = rs.getString(DBNames.TERM_VALUE);
			this.positions = new HashMap<>();
	        this.weights = new HashMap<>();
			setPositionAndWeight(rs.getString(DBNames.TERM_W_AND_P));
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
    }
    
    public int getID()
    {
    	return this.id;
    }
    
    public String getValue()
    {
        return this.value;
    }
    
    public void addPosition(int n, int p)
    {
    	ArrayList<Integer> pos;
    	if (!this.positions.containsKey(n))
    	{
    		pos = new ArrayList<>();
    		this.weights.put(n, 0.);
    	}
    	else
    	{	
    		pos = this.positions.get(n);
    	}
    	pos.add(p);
    	this.positions.put(n, pos);
    }
    
    public StringBuilder toDBString()
    {
        StringBuilder toDB = new StringBuilder("(\""+this.id+"\", \""+this.value+"\", \"");
        StringBuilder wpStr = new StringBuilder("");
        
        // Tableau de position et de poids stocke sous la forme N1W2.3P1P2P3N2P1P2P3...
        for (Integer node: this.positions.keySet())
        {
        	wpStr.append("N"+node);
        	for (Integer pos: this.positions.get(node))
        		wpStr.append("P"+pos);
        	wpStr.append("W"+this.weights.get(node));
        }
        
        toDB.append(wpStr);
        toDB.append("\"),");
        
        return toDB;
    }
    
    // N1P1P2P3P4W2.9N2P1P2P3P4W2.9
    // split N : T[0]=1P1P2P3P45W2.9, T[1]=2P1P2P3P45W2.9
    // split W : T'[0]=2P1P2P3P4, T'[1]=2.9
    // split P : T''[0]=2, T''[1]=1, T''[2]=2, ...
    public void setPositionAndWeight(String DBRecord)
    {
    	// split sur N pour recuperer les donnees par noeud
    	String[] nodes = DBRecord.split("N");
    	
    	// pour chaque tableau de pos de noeud
    	for (String node: nodes) 
    	{
    		if (node.length() > 0)
    		{
	    		String[] wp = node.split("W");
	    		
	    		// spliter le couple par P pour avoir les positions
	    		String[] positions = wp[0].split("P");
	    		
	    		double w = Double.parseDouble(wp[1]);
	    		int n = Integer.parseInt(positions[0]);
	    		ArrayList<Integer> p = new ArrayList<>();
	    		
	    		for (int i=1; i<positions.length; i++)
	    			p.add(Integer.parseInt(positions[i]));
	    		
	    		this.weights.put(n, w);
	    		// ajout du couple dans le tableau de positions
	    		this.positions.put(n, p);
    		}
    	}
    }
    
    public int getQuantity()
    {
        return this.positions.size();
    }
    
    public int getQuantityNode(int node)
    {
    	return positions.get(node).size();
    }

    public HashMap<Integer, ArrayList<Integer>> getPositions() 
    {
        return positions;
    }
    
    public HashMap<Integer, Double> getWeight()
    {
    	return this.weights;
    }
    
}
